var stepId = $CertusJsonAjax.getStepInstanceId();
var processId = $CertusJsonAjax.getProInstanceId();
var other1Grid;
$CertusFormAjax.ajax("findBugetInfo3",{"processId":processId},function (response) {
    var other1Columns = [{
        id: 'buget_phase',
        title: '阶段',
        type: 'string',
        width: 100,
        columnClass: 'text-center',
    },{
        id: 'buget_type',
        title: '预算类型',
        type: 'string',
        width: 100,
        columnClass: 'text-center',
    }, {
        id: 'buget',
        title: '预算金额',
        type: 'string',
        width: 150,
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
          }
    },{
        id: 'user_name',
        title: '申请人',
        type: 'string',
        width: 75,
        columnClass: 'text-center'
    },{
        id: 'create_time',
        title: '申请时间',
        type: 'date',
        width: 100,
        format:'yyyy-MM-dd',
        otype:'time_stamp_ms',
        columnClass: 'text-center'
    },{
        id: 'budget_begin',
        title: '使用周期',
        type: 'string',
        width: 200,
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += record.budgetBegin+"至"+record.budgetEnd;
            return content;
        }
    },{
        id: 'comments',
        title: '费用说明',
        type: 'string',
        columnClass: 'text-left'
    }, {
        id: 'operation',
        title: '操作',
        type: 'string',
        width: 50,
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-danger" onclick="delBudget(' + record.id + ');"><i class="fa fa-trash-o"></i> 删除</button>';
            return content;
        }
    }];
    var other1Option = {
        lang: 'zh-cn',
        ajaxLoad: false,
        extraWidth: 20,
        // loadURL:'url',
        exportFileName: '其他费用',
        datas: response.data,
        columns: other1Columns,
        gridContainer: 'other1Container',
        tools: ''
    };
    other1Grid = $.fn.dlshouwen.grid.init(other1Option);
    other1Grid.load();
});

function delBudget(id) {
    layer.confirm('确认删除此数据？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $CertusFormAjax.ajax("delLcBudget",{"id":id},function (response) {
            layer.msg("删除成功");
            // other1Grid.load()
            window.location.reload();
        })
    })
}

var proStage =0;
$CertusFormAjax.ajax("findJbxxs",{"processId":processId},function (response) {
    proStage = response.data.stageCode;
})


$CertusFormAjax.ajax("findlcTotalMoney",{"processId":processId,"status":1},function (response) {
    $("#totalMoney").html(response.data);
})
//立项商务预测
$CertusFormAjax.ajax("findLcBudget2",{"processId":processId,"status":1},function (response) {
    // 立项信息-项目指标
    var other2Columns = [{
        id: 'buget_phase',
        title: '阶段',
        type: 'string',
        width: 100,
        columnClass: 'text-center',
    },{
        id: 'buget_type',
        title: '预算类型',
        type: 'string',
        width: 100,
        columnClass: 'text-center',
    }, {
        id: 'buget',
        title: '预算金额',
        type: 'string',
        width: 150,
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
          }
    },{
        id: 'user_name',
        title: '申请人',
        type: 'string',
        width: 75,
        columnClass: 'text-center'
    },{
        id: 'create_time',
        title: '申请时间',
        type: 'date',
        width: 100,
        format:'yyyy-MM-dd',
        otype:'time_stamp_ms',
        columnClass: 'text-center'
    },{
        id: 'useTimeSycle',
        title: '使用周期',
        type: 'string',
        width: 200,
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += record.budgetBegin+"至"+record.budgetEnd;
            return content;
        }
    },{
        id: 'comments',
        title: '费用说明',
        type: 'string',
        columnClass: 'text-left'
    }];
    var other2Option = {
        lang: 'zh-cn',
        ajaxLoad: false,
        // loadURL:'url',
        extraWidth: 20,
        exportFileName: '其他费用',
        datas: response.data,
        columns: other2Columns,
        gridContainer: 'other2Container',
        tools: ''
    };
    var other1Grid = $.fn.dlshouwen.grid.init(other2Option);
    other1Grid.load();
});

// 项目费用报销记录
var costColumns = [{
    id: 'filenav',
    title: '项目阶段',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'filetype',
    title: '报销单号',
    width: 130,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'filelv',
    title: '报销总金额',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '￥' + value + '元';
        return content;
      }
}, {
    id: 'fileName',
    title: '申请人',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'filemsg',
    title: '审批日期',
    type: 'string',
    width: 100,
    columnClass: 'text-left',
}, {
    id: 'uname',
    title: '其他说明',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'operation',
    title: '操作',
    type: 'string',
    width: 50,
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<button class="btn btn-xs btn-primary" onclick="cost(' + record.id + ');"><i class="icon-eye-open"></i>  查看</button>';
        return content;
    }
}];
var costOption = {
    lang: 'zh-cn',
    ajaxLoad: false,
    extraWidth: 20,
    // loadURL:'url',
    exportFileName: '项目费用报销记录',
    datas: [{
        id: 1,
        filenav: '线索阶段',
        filetype: 'NJ201809091223',
        fileName: '张三测试',
        filemsg: '2010-08-02',
        filelv: '￥99999.00元',
        uname: '其他说明测试数据',
    },],
    columns: costColumns,
    gridContainer: 'costContainer',
    toolbarContainer: 'costToolBarContainer',
    tools: '',
    pageSize: 10,
    pageSizeLimit: [10, 15, 20]
};
var costGrid = $.fn.dlshouwen.grid.init(costOption);
costGrid.load();

//费用申请记录
function findCostbxInfo() {

}
function findCostDetail(id) {

}
function hideother() {
    $(".hideother").hide();
};

function showother() {
    $(".hideother").show();
};
var otherColumns = [{
    id: 'bugetStage',
    title: '项目阶段',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
},{
    id: 'bugetType',
    title: '预算类型',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
},{
    id: 'bugetMoney',
    title: '预算金额',
    width: 150,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '￥' + value + '元';
        return content;
      }
},{
    id: 'comments',
    title: '事由',
    type: 'string',
    columnClass: 'text-left',
},{
    id: 'useTimeSycle',
    title: '使用周期',
    type: 'string',
    width: 200,
    columnClass: 'text-center'
},{
    id: 'operation',
    title: '操作',
    width: 150,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record) {
        var content = '';
        content += '<button class="btn btn-xs btn-danger" onclick="deleteWarn('+record.id+');"><i class="fa fa-trash-o"></i>  删除</button>';
        return content;
    }
}];
var otherMoneyData = [];
var otherOption = {
    lang: 'zh-cn',
    ajaxLoad: false,
    extraWidth: 20,
    // loadURL:'url',
    exportFileName: '其他费用',
    datas: otherMoneyData,
    columns: otherColumns,
    gridContainer: 'otherContainer',
    toolbarContainer: 'otherToolBarContainer',
    tools: '',
    pageSize: 10,
    pageSizeLimit: [10, 15, 20]
};
var otherGrid = $.fn.dlshouwen.grid.init(otherOption);
otherGrid.load();
var om =1;
function addOtherMoney() {
    var bugetMoney =$("#bugetMoney").val();
    var comments = $("#comments").val();
    if (bugetMoney==""){
        layer.msg("预算金额不能为空",{time:1000});
        return;
    }
    if (comments==""){
        layer.msg("事由不能为空",{time:1000});
        return;
    }
    if (comments.length>120){
        layer.msg("事由不能超过120字",{time:1000});
        return;
    }
    var data = new Date();
    var datetime = $("#startTime").val();
    var startTime = new Date(datetime.replace("/", "-").replace("/", "-"));
    if (datetime!=""){
        if (startTime>data){
            layer.msg("开始日期不能大于当前日期",{time:1000});
            return;
        }
    }else{
        layer.msg("开始日期不能为空",{time:1000});
        return;
    }
    var endTime = $("#endTime").val();
    if (endTime==""){
        layer.msg("结束日期不能为空",{time:1000});
        return;
    }
    var endTimes = new Date(endTime.replace("/", "-").replace("/", "-"));
    if (startTime>endTimes){
        layer.msg("开始日期不能大于结束日期",{time:1000});
        return;
    }

    var arr = {};
    if (proStage=="1"){
        arr.bugetStage = "线索阶段"
    }else if(proStage=="2"){
        arr.bugetStage = "立项阶段"
    }
    arr.id = om;
    arr.bugetMoney = bugetMoney;
    arr.comments = comments;
    arr.bugetType = $("#bugetType").val();
    arr.startTime = $("#startTime").val();
    arr.endTime = $("#endTime").val();
    arr.useTimeSycle = $("#startTime").val()+"至"+$("#endTime").val();
    otherMoneyData.push(arr);
    om++;
    otherGrid.load();
    $(".hideother").hide();
}
function deleteWarn(wid) {
    var dArr={};
    for (var i=0;i<otherMoneyData.length;i++){
        if (otherMoneyData[i].id==wid){
            dArr = otherMoneyData[i];
        }
    }
    otherMoneyData.splice($.inArray(dArr,otherMoneyData),1);
    otherGrid.load();
};
$(".submit").on('click',function () {
    $CertusFormAjax.ajax("editBudget",{"param":JSON.stringify(otherMoneyData),"stepId":stepId,"processId":processId},function (response) {
        layer.msg("申请成功");
        window.history.back();
    })
})